<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增评估')"/>
    <th:block th:include="include :: select2-css"/>
    <th:block th:include="include :: layout-latest-css"/>

</head>
<body>
<div class="main-content">
    <form id="form-user-add" class="form-horizontal">
        <h4 class="form-header h4">基础信息</h4>
        <div class="row">
            <div class="col-sm-4">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">测评名称：</label>
                    <div class="col-sm-8">
                        <input name="aTitle" th:value="${aAttack.aTitle}" class="form-control" type="text" readonly>
                    </div>
                </div>
            </div>

            <div class="col-sm-4">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">攻击类型：</label>
                    <div class="col-sm-8">
                        <select onchange="onchangeAttackType()" id="aType" name="aType" readonly
                                class="form-control m-b"
                                th:with="type=${@dict.getType('attack_type')}">
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                    th:value="${dict.dictValue}"
                                    th:selected="${dict.dictValue == aAttack.aType}"></option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-sm-4">
                <div class="form-group">
                    <label class="col-sm-4 control-label">时间段：</label>
                    <div class="col-sm-8">
                        <input name="aTime" placeholder="" class="form-control" type="text" th:value="${aAttack.aTime}"
                               readonly>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-4">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">自定义攻击系数（DBT）：</label>
                    <div class="col-sm-8">
                        <input id="aRate" name="aRate" class="form-control" type="number" th:value="${aAttack.aRate}"
                               maxlength="11" readonly>
                    </div>
                </div>
            </div>
            <div class="col-sm-4">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">完整系数权重（q1）：</label>
                    <div class="col-sm-8">
                        <input id="aQ1" name="aQ1" class="form-control" type="number" th:value="${aAttack.aQ1}"
                               maxlength="20"
                               readonly>
                    </div>
                </div>
            </div>
            <div class="col-sm-4">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required"> 关联系数权重（q2）：</label>
                    <div class="col-sm-8">
                        <input id="aQ2" name="aQ2" class="form-control" type="number" th:value="${aAttack.aQ2}"
                               maxlength="20"
                               readonly>
                    </div>
                </div>
            </div>
        </div>

    </form>
    <h4 class="form-header h4">完整性要素 <span class="font-red" val="0.0" id="p1">p1=0.0</span></h4>


    <div class="col-sm-12 select-table table-striped">
        <table id="table1"></table>
    </div>

    <h4 class="form-header h4">关联性要素 </h4>
    <div class=" select-table table-striped col-sm-8">
        <table id="table2"></table>
    </div>

    <h4 class="form-header h4">关联要素组合比率和累计比率 <span class="font-red" val="0.0" id="p2">p1=0.0</span></h4>

    <div class=" select-table table-striped col-sm-12">
        <table id="table3"></table>
    </div>


    <h4 class="form-header h4 pull-left">专家打分</h4>

    <div class="col-sm-12 select-table table-striped">
        <table id="table4"></table>
    </div>

    <h4 class="form-header h4 pull-left">攻击概率（p） </h4>
    <div class="row  pull-left">
        <span class="font-red" id="attack-p">p=0.0</span>
    </div>


</div>

<div class="row">
    <div class="col-sm-offset-6 col-sm-10">
        <button type="button" class="btn btn-sm btn-primary" onclick="closeItem()"><i class="fa fa-backward"></i>返 回
        </button>
        </button>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: select2-js"/>
<!--<th:block th:include="include :: layout-latest-js"/>-->

<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    //--------------------------第一个表格---------------------------
    var prefix = ctx + "system/wzs";

    /* 标记状态 */
    function statusTools(row) {
        if (row.aChas == 1) {
            return '<i class=\"fa fa-toggle-on text-info fa-2x\"></i> ';

        } else {
            return '<i class=\"fa fa-toggle-off text-info fa-2x\"></i> ';

        }
    }

    //初始化第一个表格
    $(function () {
        var data = [[${aAttack.wzxsObj}]];
        setP1(data);
        var options = {
            data: data,
            modalName: "完整性要素",
            id: 'table1',
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            showSearch: false,
            pagination: false,
            columns: [
                {
                    title: "编号（n）",
                    formatter: function (value, row, index) {
                        return $.table.serialNumber(index);
                    }
                },
                {
                    field: 'aCname',
                    title: '要素名称'
                },
                {
                    field: 'aCweight',
                    title: '权重系数'
                },
                {
                    title: '标记出现',
                    align: 'center',
                    formatter: function (value, row, index) {
                        return statusTools(row);
                    }
                }]
        };
        $.table.init(options);
    });


    //计算p1的值
    function setP1(rows) {
        //计算 p1
        var p1 = 0.0;
        if (rows && rows.length > 0) {
            $(rows).each(function (i, e) {
                if (e.aChas == 1) {
                    p1 += (i + 1) * e.aCweight;
                }
            })
            p1 = p1 / rows.length;
            p1 = p1.toFixed(4)
            $('#p1').text('p1 = ' + p1);
            $('#p1').attr('val', p1)

        } else {
            $('#p1').text('p1 = ' + 0.0);
            $('#p1').attr('val', 0.0)
        }
        console.log("p1:" + p1)
        setAttackRate();

    }

    //--------------------------第一个表格结束---------------------------
</script>

<script th:inline="javascript">

    $(function () {
        var data = [[${aAttack.glxsObj}]];
        var options2 = {
            data: data,
            modalName: "关联性要素",
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            showSearch: false,
            pagination: false,
            id: 'table2',
            columns: [{
                checkbox: true,
            }, {
                title: "序号",
                formatter: function (value, row, index) {
                    return $.table.serialNumber(index);
                }
            },

                {
                    field: 'aCname',
                    title: '要素名称'
                },
                {
                    field: 'createTime',
                    title: '创建时间'
                }]
        };
        $.table.init(options2);
        initTable3(data.length);
        $('input[name=btSelectAll]').click();
        $('input[name=btSelectAll]').attr("disabled", "true");

        $('input[name=btSelectItem]').attr("disabled", "true");
    });


    //计算第三个表格
    function initTable3(count) {
        var prefix3 = ctx + "system/attack/getCanKaoData?count=" + count;
        var dataTable3;
        var options3 = {
            url: prefix3,
            modalName: "关联性要素",
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            showSearch: false,
            pagination: false,
            id: 'table3',
            onLoadSuccess: function (data) {
                dataTable3 = data;
                setP2();
            },
            columns: [
                {
                    field: 'title',
                    title: '正关联要素数目'
                }]
        };

        for (var i = 2; i <= count; i++) {
            options3.columns.push(
                {
                    field: 'c_' + i,
                    title: i
                }
            )
        }
        $.table.init(options3);
    }


</script>
<th:block th:include="include :: bootstrap-table-editable-js"/>
<script th:inline="javascript">


    $(function () {
        var data = [[${aAttack.scoresObj}]];
        var options4 = {
            data: data,
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            pagination: false,
            id: 'table4',
            columns: [
                {
                    title: "序号",
                    formatter: function (value, row, index) {
                        return $.table.serialNumber(index);
                    }
                },
                {
                    field: 'aSid',
                    title: 'id',
                    visible: false
                }, {
                    field: 'aSname',
                    title: '专家名称'
                },
                {
                    field: 'aSvalue',
                    title: '关联性要素个数',
                }
            ]
        };
        $.table.init(options4);
    });


    /* 查询表格所有数据值 */
    function getData(table) {
        var data;
        if (table) {
            data = $("#" + table).bootstrapTable('getData');
        } else {
            data = $("#table4").bootstrapTable('getData');
        }
        return data;
    }


    //计算p1的值
    function setP2() {
        //打分
        var rows = getData();

        if (rows && rows.length > 0) {
            //计算 p1
            var p2 = 0.0;
            $(rows).each(function (i, e) {
                if (e.aSvalue && e.aSvalue > 0) {
                    p2 += getRate(e.aSvalue);
                }
            })
            p2 = p2 / rows.length
            p2 = p2.toFixed(4)
            $('#p2').text('p2 = ' + p2);
            $('#p2').attr('val', p2)

        } else {
            $('#p2').text('p2 = ' + 0.0);
            $('#p2').attr('val', 0.0)
        }
        setAttackRate();
    }

    //计算概率。


    //打分的平均分
    function getRate(aSvalue) {
        var data = $("#table3").bootstrapTable('getData');

        var rate = data[0];
        var rateAll = data[1];

        var element = rateAll['c_' + aSvalue];
        console.log(element)
        if (element) {
            return parseFloat(element);
        } else {
            return 0.0;

        }
    }

    //计算最攻击概率

    function setAttackRate() {

        var q1 = $('#aQ1').val();
        var q2 = $('#aQ2').val();
        var p1 = $('#p1').attr('val');
        var p2 = $('#p2').attr('val');

        var rate = 0.5 * (q1 * p1 + q2 * p2);
        rate = rate.toFixed(4)
        $('#aRate').val(rate);
        $('#attack-p').text('p =' + rate);

    }
</script>
</body>
</html>